/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.jude.rollviewpager;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;

/**
 * CarouselPageSlider
 *
 * @since 2021-03-23
 */
public class RollPagerView extends DirectionalLayout {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x2021, "CarouselPageSlider");
    private PageSlider pageSlider = null;
    private PageSliderIndicator pageSliderIndicator = null;
    private ArrayList<Integer> pages = null;
    private int post = 0;
    private boolean isStartEnd = true; // true,轮播图默认是播放
    private final EventHandler handler = new EventHandler(EventRunner.create(true));
    private final float mScale = 0.2f;
    private final int mColor = 0xff;
    private final int mNum20 = 20;
    private final int mColor2 = 0xf2;
    private final int mColor7 = 0xa7;
    private final int mColora = 0x3a;
    private final int mNum1 = 1;
    private final int mNum2 = 2;
    private final int mNum3 = 3;
    private final int mNum4 = 4;
    private final int mNum3000 = 3000;
    private final int mNum2000 = 2000;
    private int alpha;
    private int paddingLeft;
    private int paddingTop;
    private int paddingRight;
    private int paddingBottom;
    private final Component component;

    /**
     * CarouselPageSlider
     *
     * @param context context
     */
    public RollPagerView(Context context) {
        super(context);
        component = this;
        initview();
        initdata();
    }

    /**
     * CarouselPageSlider
     *
     * @param context context
     * @param attrSet attrSet
     */
    public RollPagerView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        component = this;
        initview();
        initdata();
    }

    /**
     * 组合控件必须在代码里面重新加载数据
     */
    private void initview() {
        LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_pageslider_view, this, true);
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_pager);

        pageSliderIndicator = (PageSliderIndicator) findComponentById(ResourceTable.Id_pageindicator);
    }

    /**
     * initdata
     */
    private void initdata() {
        pageSlider.setReboundEffect(true);
        pageSlider.setSlidingPossible(true);
        pageSlider.setCircularModeEnabled(true);
        pageSlider.setCentralScrollMode(true);
        ShapeElement shape = new ShapeElement();
        shape.setCornerRadius(AttrHelper.vp2px(mNum20, getContext()));
        shape.setRgbColor(new RgbColor(mColor, mColor, mColor));
        ShapeElement shape1 = new ShapeElement();
        shape1.setCornerRadius(AttrHelper.vp2px(mNum20, getContext()));
        shape1.setRgbColor(new RgbColor(mColor2, mColor7, mColora));
        /**
         * 设置选中和未选中属性
         */
        pageSliderIndicator.setItemElement(shape, shape1);
        pageSliderIndicator.setItemOffset(AttrHelper.vp2px(mNum20, getContext()));
        pageSliderIndicator.setScale(mScale, mScale);
        /**
         * 将指示器和pageslider绑定
         */
        pageSliderIndicator.setPageSlider(pageSlider);
        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int position, float offset, int offsetPix) {
            }

            @Override
            public void onPageSlideStateChanged(int ptd) {
            }

            @Override
            public void onPageChosen(int index) {
                /**
                 * 选中时会调用这个方法
                 */
                post = index;
            }
        });
        initHandler();
    }

    /**
     * initHandler
     */
    private void initHandler() {
        handler.postTask(new Runnable() {
            @Override
            public void run() {
                getUihand();
                handler.postTask(this, mNum3000); // 图片执行的间隔
            }
        }, mNum2000); // 第一次加载时延迟的时间
    }

    private void getUihand() {
        getContext().getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                aloading();
            }
        });
    }

    private void aloading() {
        if (isStartEnd) {
            int cut = post + 1; // 下一个轮播页面
            int num = pages == null ? 0 : pages.size();
            if (cut >= num) {
                cut = 0; // 如果超过长度,则回到起始点
            }
            pageSlider.setCurrentPage(cut, Until.isUge()); // true表示平滑滚动，false表示立即滚动。
        }
    }

    /**
     * 设置适配器和轮播的图片
     *
     * @param pageSliderAdapter 适配器
     * @param page              带图片的list集合
     */
    public void setadapter(PageSliderProvider pageSliderAdapter, ArrayList<Integer> page) {
        pageSlider.setProvider(pageSliderAdapter);
        this.pages = page;
    }

    /**
     * 获取当前的页数
     *
     * @return return
     */
    public int getpost() {
        return post;
    }

    /**
     * 设置提示view的位置
     *
     * @param left   左边距
     * @param top    q
     * @param right  q
     * @param bottom q
     */
    public void setHintPadding(int left, int top, int right, int bottom) {
        paddingLeft = left;
        paddingTop = top;
        paddingRight = right;
        paddingBottom = bottom;
        component.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
    }

    /**
     * 设置当前页数
     *
     * @param tag 参数
     */
    public void setNext(int tag) {
        if (Until.isUge()) {
            pageSlider.setCurrentPage(tag, false);
        } else {
            pageSlider.setCurrentPage(tag);
        }
    }

    /**
     * 修改list
     */
    public void setList() {
        pages.set(0, ResourceTable.Media_img1);
        pages.set(mNum1, ResourceTable.Media_img2);
        pages.set(mNum2, ResourceTable.Media_img3);
        pages.set(mNum3, ResourceTable.Media_img4);
        pages.set(mNum4, ResourceTable.Media_img5);
    }

    /**
     * 暂停或者播放.true播放,flase是暂停,默认播放
     */
    public void setStartEnd() {
        this.isStartEnd = !isStartEnd;
    }

    /**
     * 设置提示view的透明度
     *
     * @param alp 0为全透明  255为实心
     */
    public void setHintAlpha(int alp) {
        this.alpha = alp;
        pageSlider.setAlpha(alpha);
    }
}
